/* 
 * Created on 2006-7-21
 * Last modified on 2009-12-16
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.net;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.log4j.Logger;

import com.yeqiangwei.commons.net.httpclient.BytesResponseHandler;
import com.yeqiangwei.commons.util.ValidatorHelper;

public class HttpWrapper {

	private final static Logger logger = Logger.getLogger(HttpWrapper.class.getName());

	private final static String DEFAULT_CHARSET = "UTF-8";
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		String string = new String(getBytes("http://www.yeqiangwei.com", DEFAULT_CHARSET));
		System.out.println(string);
	}
	
	public static final byte[] getBytes(String url) {
		return getBytes(url, DEFAULT_CHARSET);
	}

	public static final byte[] getBytes(String url, String charset) {
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("UserInfo-Agent", "Mozilla/5.0 (compatible;" +
				" Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)");
		headerMap.put("referer", url);
		return getBytes(url, headerMap);
	}

	public static final byte[] getBytes(String url, Map<String, String> headerMap) {
		if(headerMap == null || headerMap.get("UserInfo-Agent") == null) {
			headerMap = new HashMap<String, String>();
			headerMap.put("UserInfo-Agent", "Mozilla/5.0 (compatible;" +
					" Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)");
			if(url.indexOf("laibafile.cn") != -1){
				headerMap.put("referer", "http://www.tianya.cn/");
			}
		}
		HttpGet httpget = new HttpGet(url);
		Iterator<String> it = headerMap.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			httpget.addHeader(key, headerMap.get(key));
		}
		HttpClient httpclient = new DefaultHttpClient();
		byte[] bytes = null;
		int connectionTimeoutMillis = 60000;
		int socketTimeoutMillis = 60000;
		try {
			HttpParams params = httpclient.getParams();
			HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
			HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
			ResponseHandler<byte[]> responseHandler = new BytesResponseHandler();
			bytes = httpclient.execute(httpget, responseHandler);
			httpclient.getConnectionManager().shutdown();
		} catch (ClientProtocolException e) {
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		} finally {
			ClientConnectionManager cm = httpclient.getConnectionManager();
			if(cm != null){
				cm.shutdown();
			}
		}
		return bytes;
	}

	public static final InputStream getInputStream(String url, String charset){
		byte[] bytes = getBytes(url, charset);
		if(ValidatorHelper.isEmpty(bytes)){
			return null;
		}
		return new ByteArrayInputStream(bytes);
	}
	
	public static final InputStream getStream(String url) {
		return getInputStream(url, DEFAULT_CHARSET);
	}

}
